import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:ruhun_verify/ruhun_verify_method_channel.dart';

import 'package:flutter/services.dart';
final GlobalKey<NavigatorState> navigatorStateKey = GlobalKey<NavigatorState>();

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      RuHunVerifyChannel.initLoginWithId('1400685703', 5000, '入魂大集');
    } else {
      RuHunVerifyChannel.initLoginWithId('1400626669', 5000, '入魂大集').then((value) {
        debugPrint('getTokenCallback ${value.runtimeType}= $value');
      });
    }
    RuHunVerifyChannel.getTokenCallback((result) {
      debugPrint('getTokenCallback = $result');
    });
    RuHunVerifyChannel.getSmsLoginCallback((result) {
      debugPrint('getSmsLoginCallback = $result');
    });

    RuHunVerifyChannel.getUnCheckDialogCallback((result){
      debugPrint('getSmsLoginCallback = $result');
    });

    initPlatformState();
  }


  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await RuHunVerifyChannel.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorStateKey,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion\n'),
              TextButton(
                  onPressed: () {
                    RuHunVerifyChannel.getSimInfo().then((value) {
                      debugPrint('获取运营商信息----------$value');
                    });
                  },
                  child: const Text('获取运营商信息')),
              TextButton(
                  onPressed: () {
                    RuHunVerifyChannel.preLoginWithBack().then((value) {
                      debugPrint('${value.runtimeType}');

                      debugPrint('预登录----------$value');
                    });
                  },
                  child: const Text('预登录')),
              TextButton(
                  onPressed: () {
                    RuHunVerifyChannel.loginWithController('入魂大集').then((error) {
                      debugPrint('${error.runtimeType}');
                      debugPrint('拉起授权页面失败去短信----------$error');
                    }, onError: (e) {
                      debugPrint('安卓拉起授权页失败:${(e as PlatformException).message}');
                    });
                  },
                  child: const Text('拉起授权页面')),
            ],
          ),
        ),
      ),
    );
  }
}
